# _*_ coding: utf8 _*_
import hashlib

from django.http import HttpResponse
from django.shortcuts import render

from iyinyue.settings import VERSION


def home(request):
    return render(request, 'home.html', {})


def video(request):
    return render(request, 'video.html', {})


def luoyin(request):
    return render(request, 'luoyin.html', {'version': VERSION})


def ly_teachers(request):
    return render(request, 'ly_teachers.html', {'version': VERSION})


def ly_courses(request):
    return render(request, 'ly_courses.html', {'version': VERSION})


def ly_environment(request):
    return render(request, 'ly_environment.html', {'version': VERSION})


def weixin(request):
    signature = request.GET.get('signature', '')
    timestamp = request.GET.get('timestamp', '')
    nonce = request.GET.get('nonce', '')
    echostr = request.GET.get('echostr', '')

    answer = ''.join(sorted([timestamp, nonce,
                             'wuxinting']))
    shaanswer = hashlib.sha1(answer).hexdigest()

    if shaanswer == signature:
        return HttpResponse(echostr)
    else:
        return HttpResponse('error')
